/*
 * 文件名：StackWithMin.java
 * 描述： StackWithMin.java
 * 修改人：Lo&Wang
 * 修改时间：2016年11月28日
 * 修改内容：新增
 */
package com.lowang.test.queue;

import java.util.Stack;

/**
 * 返回栈最小值
 * @author zhengmo
 */
public class StackWithMin {
    private Stack<Integer> data;
    private Stack<Integer> min;
    public StackWithMin() {
        data = new Stack<>();
        min = new Stack<>();
    }
    public void push(Integer num) {
        data.push(num);
        if (min.isEmpty()) {
            min.push(num);
        } else {
            if (num > min.peek()) {
                min.push(Integer.valueOf(min.peek()));
            } else {
                min.push(num);
            }
        }
    }
    public Integer pop() {
        Integer i = data.pop();
        min.pop();
        return i;
    }
    public Integer min() {
        if (min.isEmpty()) {
            return null;
        }
        return min.peek();
    }
    public boolean isEmpty() {
        return data.isEmpty();
    }
    public static void main(String[] args) {
        StackWithMin stack = new StackWithMin();
        stack.push(10);
        stack.push(6);
        stack.push(1);
        stack.push(3);
        stack.push(9);
        stack.push(0);
        while (!stack.isEmpty()) {
            System.out.println("min:" + stack.min());
            System.out.println(stack.pop());
        }
    }
}
